package com.ruge.test.Velocity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.junit.Test;

import java.io.StringWriter;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 爱丽丝、如歌  创建于 2018/7/10 20:09
 * 说明:  TODO
 */
public class HelloVelocity {
    @Test
    public void test1(){
        // 初始化模板引擎
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        // 获取模板文件
        Template t = ve.getTemplate("velocity/hellovelocity.vm");
        // 设置变量
        VelocityContext ctx = new VelocityContext();
        ctx.put("name", "Velocity");
        List list = new ArrayList();
        list.add("1");
        list.add("2");
        ctx.put("list", list);
        // 输出
        StringWriter sw = new StringWriter();
        t.merge(ctx,sw);
        System.out.println(sw.toString());
    }

    @Test
    public void test2(){
        DateFormat df2 = DateFormat.getDateTimeInstance();

        Date date = new Date(1531318207000L);
        System.out.println(df2.format(date));
        Date date1 =new Date(1531324728910L);
        System.out.println(df2.format(date1));
        Date date2 =new Date(1531366621000L);
        System.out.println(df2.format(date2));
    }
}
